<template>
  <div  class="sign">
    <div class="from-panel">
      <el-form label-position="left" ref="form" label-width="80px" :rules="rules" :model="user">
        <el-form-item label="昵称" prop="name">
          <el-input v-model="user.name"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input type="password" v-model="user.pwd"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="cpwd">
          <el-input type="password" v-model="user.cpwd"></el-input>
        </el-form-item>

        <div class="oprator">
          <el-button class="submit"  @keyup.enter="submitForm('form')" @click="submitForm('form')">提交</el-button>
        </div>
      </el-form>
    </div>
    <div class="sign-text">
      <span  class="text">
       <p class="title">简心网站，常联系！</p>
        2018年，简心网站诞生<br>

        今天，你可以用它来书写印迹<br>

         现在，您也可以在文章里<br>

         用:-)来传达一个微笑的表情
      </span>
      <span  class="img"><img src="../assets/img/icon-heart.jpeg" alt=""></span>
    </div>
  </div>
</template>

<script>
  export default {

    name: 'signUp',

    data() {

      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.user.cpwd !== '') {
            this.$refs.form.validateField('cpwd');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.user.pwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };

      return {
        labelPosition: 'left',
        user: {
          name: '',
          email: '',
          pwd: '',
          cpwd: '',
        },
        rules: {
          name: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 7 个字符之间', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请填入邮箱', trigger: 'blur' },
            { type: 'email', message: '必须是合法的邮箱格式', trigger: 'blur' }
          ],
          pwd: [
            { required: true, message: '必须填写', trigger: 'blur' },
            { validator: validatePass, trigger: 'blur' },
          ],
          cpwd: [
            { required: true, message: '必须填写', trigger: 'blur' },
            { validator: validatePass2, trigger: 'blur' },
          ]
        }
      };
    },
    mounted(){
      this.$Progress.finish();
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let user = new this.$api.SDK.User();
            user.setUsername(this.user.name);
            user.setPassword(this.user.pwd);
            user.setEmail(this.user.email);

            user.signUp().then((loginUser) => {
              this.$store.dispatch('login', loginUser); // 保存到 Vuex 中
              this.$message.success("注册成功！")
              this.$router.push({path: '/signIn'});//跳转到登录页面
            }).catch(error => {
              console.error(error)
              this.$message.error(error.message)
            })
          } else {
            console.log('error submit!!');
            this.$message.error('错了哦，您填写的信息有错误，请按照提示修改。');
            return false;
          }
        });
      },
    }
  };
</script>

<style lang="less" scoped>
  .from-panel{
    width: 50% !important;
    margin: 0 auto!important;
    margin-top: 100px!important;
  }
  .oprator{
    margin-top: 30px!important;
    text-align: center!important;
  }
  .submit{
    background-color: #444444!important;
    color: #ffffff!important;
  }

  .sign-text{
    margin: 0  auto;
    margin-top: 20px;
    text-align: center;
    .text{
      display: inline-block;
      text-align: left;
      padding-top: 25px;
      font-size: 14px;
      font-weight: normal;
      line-height: 24px;
      .title{
        line-height: 26px;
        margin-top: 15px;
        margin-bottom: 15px;
        font-size: 24px;
        font-weight: normal;
        color: #807060;

      }
    }
  }
</style>
